package v1

import (
	"chat/internal/common/request"
	"chat/internal/common/response"
	"chat/internal/service"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func Test(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"msg": "ok",
	})
}

func GetMessages(c *gin.Context) {
	var messageRequest request.MessageRequest
	err := c.BindQuery(&messageRequest)
	if nil != err {
		fmt.Printf("%+v", err)
	}
	messages, err := service.GetPrivateMessage(messageRequest)
	if err == nil {
		c.JSON(http.StatusOK, response.SuccessMsg(messages))
		return
	}

	c.JSON(http.StatusOK, response.FailMsg("GetMessages failed"))
	return
}

// GetAllMessagesByUId
// 获取用户所有的相关消息
func GetAllMessagesByUId(c *gin.Context) {
	idStr := c.Query("uid")
	id, _ := strconv.Atoi(idStr)
	// 获取所有私信
	messages, err := service.GetAllMessByUId(id)
	// 获取所在的群组id
	list, err := service.GetGroupsByUserId(id)
	// 再获取群组的所有的消息
	groupMessages, err := service.GetGroupMessageByGroupId(list)
	messages = append(messages, groupMessages...)
	if err == nil {
		c.JSON(http.StatusOK, response.SuccessMsg(messages))
		return
	}
	c.JSON(http.StatusOK, response.FailMsg("GetAllMessagesByUId failed"))
	return
}
